//
//  RecommendViewModel.swift
//  DouYu
//
//  Created by 王鹏宇 on 2018/4/11.
//  Copyright © 2018年 王鹏宇. All rights reserved.
//

import UIKit

/*
 1> 请求0/1数组,并转成模型对象
 2> 对数据进行排序
 3> 显示的HeaderView中内容
 */

class RecommendViewModel: BaseViewModel {

    //MARK: - 懒加载属性
    lazy var cycleModels : [CycleModel] = [CycleModel]()
    fileprivate lazy var bigDataGroup : AnchorGroup = AnchorGroup()
    fileprivate lazy var prettyGroup : AnchorGroup = AnchorGroup()
}

//MARK:- 发送网络请求

extension RecommendViewModel {
    
    //请求推荐数据
    func requestData(_ finishCallback : @escaping () -> ()) {
        
        //1 定义参数
        let parameters = ["limit" : "4", "offset" : "0","client_sys" : "ios", "time" : NSDate.getCurrentTime()]
        //2 创建Group
        let dGroup = DispatchGroup()
        
        //3 请求第一部分的数据
        dGroup.enter()
        NetworkManager.requestData(.post, URLString: "http://capi.douyucdn.cn/api/v1/getbigDataRoom",parameters: ["client_sys" : "ios","time" : NSDate.getCurrentTime()]) { (result) in

            //1 将result装成字典类型
            guard let  resultDict = result as? [String : Any] else{ return }
            //2 根据data key 获取数组
            guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }

            //3 遍历字典，并且装成模型对象
            self.bigDataGroup.tag_name = "最热"
            self.bigDataGroup.icon_name = "home_header_hot"

            //获取主播数据
            for dict in dataArray {

                guard let data = try?
                    JSONSerialization.data(withJSONObject:dict, options:.prettyPrinted) else { return }

                guard let model = try? JSONDecoder().decode(AnchorModel.self, from: data) else { return }

                self.bigDataGroup.room_list!.append(model)
            }
            //离开组
            dGroup.leave()
        }
        
        //  请求第二部分数据
        
        dGroup.enter()
        NetworkManager.requestData(.get, URLString: "http://capi.douyucdn.cn/api/v1/getVerticalRoom",parameters: parameters) { (result) in

            //将result转成字典类型
            guard let resultDict = result as? [String : NSObject] else { return }
            //根据data该 key 获取值
            guard let dataArray = resultDict["data"] as? [[String : NSObject]] else {return}

            //遍历字典，并转成模型数组
            self.prettyGroup.tag_name = "颜值"
         //   self.prettyGroup.icon_name = "columnYanzhiIcon"

            //获取主播数据
            for dict in dataArray {
                guard let data = try?
                    JSONSerialization.data(withJSONObject:dict, options:.prettyPrinted) else { return }

                guard let model = try? JSONDecoder().decode(AnchorModel.self, from: data) else { return }
                self.prettyGroup.room_list?.append(model)
            }

            dGroup.leave()
            }
        
        
        dGroup.enter()
        loadAnchorData(isGroupData: true, URLString: "http://capi.douyucdn.cn/api/v1/getHotCate", parameters: parameters) {
            self.anchorGroups.remove(at: 1)
            dGroup.leave()
        }
        // 所有数据都请求到之后进行排序
        dGroup.notify(queue: DispatchQueue.main){
            
            self.anchorGroups.insert(self.prettyGroup, at: 0)
            self.anchorGroups.insert(self.bigDataGroup, at: 0)
            
            finishCallback()
        }
        
        }
    
    
    //请求无线轮播的数据
    func requestCycleData(_ finishCallback : @escaping () -> ()){
        
        NetworkManager.requestData(.get, URLString: "http://capi.douyucdn.cn/api/v1/slide/6",parameters: ["version" : "2.401"]) { (result) in
            
            //获取整体字典数据
            guard let resultDict = result as? [String : Any] else { return }
            
            //根据data key 获取数据
            guard let dataArray = resultDict["data"] as? [[String : Any]] else { return }
            
            //字典转模型
            for dict in dataArray {
                
                guard let data = try?
                    JSONSerialization.data(withJSONObject:dict, options:.prettyPrinted) else { return }
                
                guard let model = try? JSONDecoder().decode(CycleModel.self, from: data) else { return }
                
                self.cycleModels.append(model)
            }
            
            finishCallback()
        }
    }
}
